package com.lagou.service.impl;

import com.lagou.service.PositionService;
import com.lagou.util.DBHelper;
import jdk.internal.dynalink.linker.LinkerServices;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.bulk.BackoffPolicy;
import org.elasticsearch.action.bulk.BulkProcessor;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;

@Service
@Slf4j
public class PositionServiceImpl implements PositionService {

    @Autowired
    private RestHighLevelClient client;

    private static final String POSITION_INDEX = "position";

    @Override
    public List<Map<String, Object>> searchPos(String keyword, int pageNo, int pageSize) throws IOException {
        if(pageNo <= 1){
            pageNo = 1;
        }
        // 搜索
        SearchRequest searchRequest = new SearchRequest(POSITION_INDEX);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //分页设置
        searchSourceBuilder.from((pageNo - 1) * pageSize);
        searchSourceBuilder.size(pageSize);
        MatchQueryBuilder builder = QueryBuilders.matchQuery("positionName", keyword);
        searchSourceBuilder.query(builder);
        searchSourceBuilder.timeout(TimeValue.timeValueSeconds(60));
        //执行搜索
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHit[] hits = response.getHits().getHits();
        List<Map<String, Object>> list = new ArrayList<>();
        for (SearchHit hit : hits) {
            list.add(hit.getSourceAsMap());
        }
        //如果检索到的职位信息不够5条 则需要启用positionAdvantage 查找 美女多、员工福利好 的企业职位信息进行补充够5条。
        if(list.size() < 5){
            list.addAll(searchPositionAdvantage(5 - list.size()));
        }
        return list;
    }

    public List<Map<String, Object>> searchPositionAdvantage(int pageSize) throws IOException {
        // 搜索
        SearchRequest searchRequest = new SearchRequest(POSITION_INDEX);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //分页设置
        searchSourceBuilder.size(pageSize);
        MatchQueryBuilder builder = QueryBuilders.matchQuery("positionAdvantage", "美女多 员工福利好");
        searchSourceBuilder.query(builder);
        searchSourceBuilder.timeout(TimeValue.timeValueSeconds(60));
        //执行搜索
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHit[] hits = response.getHits().getHits();
        List<Map<String, Object>> list = new ArrayList<>();
        for (SearchHit hit : hits) {
            list.add(hit.getSourceAsMap());
        }
        return list;
    }

    @Override
    public void importAll() throws Exception {
        writeMysqlDataToES("position");
    }

    private void writeMysqlDataToES(String tableName) throws SQLException {
        BulkProcessor bulkProcessor = getBulkProcessor(client);
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = DBHelper.getConn();
            log.info("start handle data:" + tableName);
            String sql = "select * from " + tableName;
            ps = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            //根据自己需要设置fetchSize
            ps.setFetchSize(20);
            rs = ps.executeQuery();
            ResultSetMetaData colData = rs.getMetaData();
            List<Map<String,String>> dataList = new ArrayList<>();
            HashMap<String,String> map = null;
            int count = 0;
            //c就是列的名字 v就是列对应的值
            String c = null;
            String v = null;
            while (rs.next()){
                count++;
                map = new HashMap<>(128);
                for (int i = 1; i < colData.getColumnCount(); i++) {
                    c = colData.getColumnName(i);
                    v = rs.getString(c);
                    map.put(c,v);
                }
                dataList.add(map);
                //每1万条进行一次提交，不足的批次数据最后一次提交处理
                if (count % 10000 == 0){
                    log.info("mysql handle data number:"+count);
                    //将数据添加到BulkProcessor
                    for (Map<String, String> hashMap2 : dataList) {
                        bulkProcessor.add(new IndexRequest(POSITION_INDEX).source(hashMap2));
                    }
                    //每提交一次 清空 map和datalist
                    map.clear();
                    dataList.clear();
                }
            }
            // 处理未提交的数据
            if (dataList.size() > 0){
                //将数据添加到BulkProcessor
                for (Map<String, String> hashMap2 : dataList) {
                    bulkProcessor.add(new IndexRequest(POSITION_INDEX).source(hashMap2));
                }
                //每提交一次 清空 map和datalist
                map.clear();
                dataList.clear();
                bulkProcessor.flush();
            }
        } catch (SQLException throwables) {
            log.error(throwables.getMessage(),throwables);
        } finally {
            try {
                rs.close();
                ps.close();
                conn.close();
                boolean terinaFlag = bulkProcessor.awaitClose(150L,TimeUnit.SECONDS);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private BulkProcessor getBulkProcessor(RestHighLevelClient client) {
        BulkProcessor bulkProcessor = null;
        try {
            BulkProcessor.Listener listener = new BulkProcessor.Listener() {
                @Override
                public void beforeBulk(long executionId, BulkRequest request) {
                    log.info("Try to insert data number : "
                            + request.numberOfActions());
                }
                @Override
                public void afterBulk(long executionId, BulkRequest request,
                                      BulkResponse response) {
                    log.info("************** Success insert data number : "
                            + request.numberOfActions() + " , id: " +
                            executionId);
                }
                @Override
                public void afterBulk(long executionId, BulkRequest request,
                                      Throwable failure) {
                    log.error("Bulk is unsuccess : " + failure + "executionId: "
                            + executionId);
                }
            };
            BiConsumer<BulkRequest, ActionListener<BulkResponse>> bulkConsumer =
                    (request, bulkListener) -> client
                            .bulkAsync(request, RequestOptions.DEFAULT, bulkListener);
            BulkProcessor.Builder builder = BulkProcessor.builder(bulkConsumer,
                    listener);
            builder.setBulkActions(5000);
            builder.setBulkSize(new ByteSizeValue(100L, ByteSizeUnit.MB));
            builder.setConcurrentRequests(10);
            builder.setFlushInterval(TimeValue.timeValueSeconds(100L));
            builder.setBackoffPolicy(BackoffPolicy.constantBackoff(TimeValue.timeValueSeconds(1L), 3));
            // 注意点：让参数设置生效
            bulkProcessor = builder.build();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                bulkProcessor.awaitClose(100L, TimeUnit.SECONDS);
            } catch (Exception e1) {
                log.error(e1.getMessage());
            }
        }
        return bulkProcessor;
    }

}
